<template>
  <div class="storeAdviser">
    <div class="storeAdviser_box">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model:loading="loading" :finished="finished" :finished-text="finishedText" @load="onLoad">
          <div class="storeAdviser_box_item" v-for="(item, index) in list" :key="index">
            <div class="storeAdviser_box_item_img">
              <img v-if="item.avatar" :src="item.avatar" alt="">
              <img v-else src="../../../../../assets/img/avatar1.png" alt="">
            </div>
            <div class="storeAdviser_box_item_content">
              <div class="storeAdviser_box_item_content_name">{{ item.name }}</div>
              <div class="storeAdviser_box_item_content_time">{{ item.phonenumber }}</div>
            </div>
          </div>
        </van-list>
      </van-pull-refresh>

      <van-empty v-if="list.length == 0" image-size="5rem" description="暂无顾问数据" />
    </div>
  </div>
</template>

<script setup>
import { getStoreAdviserListAPI } from '@/api/store';
import { ref } from 'vue';

const props = defineProps({
  storeId: {
    type: Number,
    default: 0
  }
})

// 顾问列表
const list = ref([])

const pageNum = ref(1)
const pageSize = ref(10)
const finishedText = ref('')
const loading = ref(false)      // 是否处于加载状态
const finished = ref(false)     // 是否已加载完毕
const refreshing = ref(false)   // 是否处于下拉刷新状态

// 获取顾问列表
const onLoad = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    storeId: props.storeId
  }
  await getStoreAdviserListAPI(params).then(res => {
    console.log(res, '顾问列表')
    list.value = list.value.concat(res.rows)
    pageNum.value++

    finishedText.value = res.total !== 0 ?res.total + ' 位顾问':''

    if (res.total <= list.value.length) {
      finished.value = true
    } else {
      finished.value = false
    }

  }).catch(err => {
    console.log(err)
    finished.value = true
  }).finally(() => {
    loading.value = false
    refreshing.value = false
  })
}

// 下拉刷新
const onRefresh = () => {
  pageNum.value = 1
  list.value = []

  finished.value = false
  loading.value = true
  onLoad()
}

</script>

<style lang="scss" scoped>
.storeAdviser {
  &_box {
    &_item {
      display: flex;
      align-items: center;
      padding: .2667rem 0;
      border-bottom: 1px solid #f8f8f8;

      &_img {
        width: 1.3333rem;
        height: 1.3333rem;
        border-radius: 50%;
        overflow: hidden;
        margin-right: .4rem;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }

      &_content {
        flex: 1;

        &_name {
          font-size: .4rem;
          color: #333;
          margin-bottom: .16rem;
        }

        &_time {
          font-size: .32rem;
          color: #999;
        }
      }
    }
  }
}
</style>